package com.xa.xamall.controller;

import com.google.common.collect.Maps;
import com.xa.xamall.framework.common.PropertiesUtil;
import com.xa.xamall.framework.vo.R;
import com.xa.xamall.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    FileService fileService;
    @GetMapping("/test")
    public ModelAndView test(){
        return new ModelAndView();
    }

    @RequestMapping("/api/upload")
    @ResponseBody
    public R upload(@RequestParam(value = "upload_file",required = false) MultipartFile file, HttpServletRequest request){
            String path = request.getSession().getServletContext().getRealPath("upload");
            String targetFileName = fileService.upload(file,path);
            String url = PropertiesUtil.getProperty("ftp.server.http.prefix")+targetFileName;
            Map fileMap = Maps.newHashMap();
            fileMap.put("uri",targetFileName);
            fileMap.put("url",url);
            return R.ok(fileMap);
    }
}
